home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / Blink / Blink.class (.txt) next >
Encoding:
Java Class File  |  1995-10-12  |  2.7 KB  |  77 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.FontMetrics;
  7. import java.awt.Graphics;
  8. import java.util.StringTokenizer;
  9.  
  10. public class Blink extends Applet implements Runnable {
  11.    Thread blinker;
  12.    String lbl;
  13.    Font font;
  14.    int speed;
  15.  
  16.    public void init() {
  17.       this.font = new Font("TimesRoman", 0, 24);
  18.       String att = ((Applet)this).getParameter("speed");
  19.       this.speed = att == null ? 400 : 1000 / Integer.valueOf(att);
  20.       att = ((Applet)this).getParameter("lbl");
  21.       this.lbl = att == null ? "Blink" : att;
  22.    }
  23.  
  24.    public void paint(Graphics g) {
  25.       int x = 0;
  26.       int y = this.font.getSize();
  27.       int red = (int)(Math.random() * (double)50.0F);
  28.       int green = (int)(Math.random() * (double)50.0F);
  29.       int blue = (int)(Math.random() * (double)256.0F);
  30.       Dimension d = ((Component)this).size();
  31.       g.setColor(Color.black);
  32.       g.setFont(this.font);
  33.       FontMetrics fm = g.getFontMetrics();
  34.       int space = fm.stringWidth(" ");
  35.  
  36.       int w;
  37.       for(StringTokenizer t = new StringTokenizer(this.lbl); t.hasMoreTokens(); x += w) {
  38.          String word = t.nextToken();
  39.          w = fm.stringWidth(word) + space;
  40.          if (x + w > d.width) {
  41.             x = 0;
  42.             y += this.font.getSize();
  43.          }
  44.  
  45.          if (Math.random() < (double)0.5F) {
  46.             g.setColor(new Color((red + y * 30) % 256, (green + x / 3) % 256, blue));
  47.          } else {
  48.             g.setColor(Color.lightGray);
  49.          }
  50.  
  51.          g.drawString(word, x, y);
  52.       }
  53.  
  54.    }
  55.  
  56.    public void start() {
  57.       this.blinker = new Thread(this);
  58.       this.blinker.start();
  59.    }
  60.  
  61.    public void stop() {
  62.       this.blinker.stop();
  63.    }
  64.  
  65.    public void run() {
  66.       while(true) {
  67.          try {
  68.             Thread.currentThread();
  69.             Thread.sleep((long)this.speed);
  70.          } catch (InterruptedException var1) {
  71.          }
  72.  
  73.          ((Component)this).repaint();
  74.       }
  75.    }
  76. }
  77.